package com.h117121.fashionshop.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;

import com.h117121.fashionshop.controller.generic.GenericController;
import com.h117121.fashionshop.core.dom.DepositCard;
import com.h117121.fashionshop.validator.DepositValidator;

@Controller
@RequestMapping("/deposit")
public class DepositController extends GenericController {
	@Autowired
	private DepositValidator validator;

	@RequestMapping(method = RequestMethod.POST)
	public String processFormSubmit(HttpServletRequest request,
			@ModelAttribute("depositcard") DepositCard depositcard,
			BindingResult errors, SessionStatus status) {
		validator.validate(depositcard, errors);
		if (!errors.hasErrors()) {
			return "LoginPage";
		}

		return "DepositPage";
	}

	@RequestMapping(method = RequestMethod.GET)
	public String showPage(HttpServletRequest request, ModelMap model) {
		if (request.getSession().getAttribute("account") == null) {
			return "redirect:/login";
		} else {
			model.addAttribute("depositcard", new DepositCard());
			return "DepositPage";
		}
	}
}
